<?php
namespace Org\Util;
class Getui{
	public $APPKEY = '';
	public $APPID = '';
	public $MASTERSECRET = '';
	public $HOST = '';
	public $CID = '';
	public function __construct(){
		vendor('getui.IGt#Push');
		$this->APPKEY 	  		= 'SqSC6d8bNl9w3zhR0o1xW2';
		$this->APPID 		  	= '4nNsBZiVYd7kf3HR3hkRJ4';
		$this->MASTERSECRET 	= 'LoT1dwpIak9TKRv5PsIxo1';
		$this->HOST 		  	= 'http://sdk.open.api.igexin.com/apiex.htm';
		$this->CID = '5c602138ab324ee64a792d2a22a418f9';
		// vendor('getui.igetui.IGt#AppMessage');
		// vendor('getui.igetui.IGt#APNPayload');
		// vendor('getui.igetui.template.IGt#BaseTemplate');
		// vendor('getui.IGt#Batch');
		// vendor('getui.igetui.utils.AppConditions');
		// $this->pushMessageToApp();
	}
	//对指定应用群推消息
	public function pushMessageToApp($title='',$text=''){
	    $igt 		  = new \IGeTui($this->HOST,$this->APPKEY,$this->MASTERSECRET);
	    //定义透传模板，设置透传内容，和收到消息是否立即启动启用
	    $template = $this->IGtNotificationTemplateDemo($this->APPID,$this->APPKEY,$title,$text);
	    //$template = IGtLinkTemplateDemo();
	    // 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
	    $message = new \IGtAppMessage();
	    $message->set_isOffline(true);
	    $message->set_offlineExpireTime(10 * 60 * 1000);//离线时间单位为毫秒，例，两个小时离线为3600*1000*2
	    $message->set_data($template);

	    $appIdList=array($this->APPID);
	    $phoneTypeList=array('ANDROID');
	    $provinceList=array('浙江');
	    $tagList=array('haha');
	    //用户属性
	    //$age = array("0000", "0010");
	    //$cdt = new AppConditions();
	    //$cdt->addCondition(AppConditions::PHONE_TYPE, $phoneTypeList);
	    //$cdt->addCondition(AppConditions::REGION, $provinceList);
	    //$cdt->addCondition(AppConditions::TAG, $tagList);
	    //$cdt->addCondition("age", $age);

	    $message->set_appIdList($appIdList);
	    //$message->set_conditions($cdt->getCondition());

	    $rep = $igt->pushMessageToApp($message,"任务组名");

	    var_dump($rep);
	    echo ("<br><br>");
	}

	//单推接口案例
	public function pushMessageToSingle(){
	    $igt = new \IGeTui($this->HOST,$this->APPKEY,$this->MASTERSECRET);
	    // $CID = '5c602138ab324ee64a792d2a22a418f9';
	    //消息模版：
	    // 4.NotyPopLoadTemplate：通知弹框下载功能模板
	    $template = $this->IGtNotyPopLoadTemplateDemo();

		// define('Alias','15068186771');
	    //定义"SingleMessage"
	    $message = new \IGtSingleMessage();

	    $message->set_isOffline(true);//是否离线
	    $message->set_offlineExpireTime(60*1*1000);//离线时间
	    $message->set_data($template);//设置推送消息类型
	    $message->set_PushNetWorkType(1);//设置是否根据WIFI推送消息，2为4G/3G/2G，1为wifi推送，0为不限制推送
	    //接收方
	    $target = new \IGtTarget();
	    $target->set_appId($this->APPID);
	    $target->set_clientId($this->CID);
	    // $target->set_alias(Alias);

	    try {
	        $rep = $igt->pushMessageToSingle($message, $target);
	        var_dump($rep);
	        echo ("<br><br>");

	    }catch(RequestException $e){
	        $requstId =e.getRequestId();
	        //失败时重发
	        $rep = $igt->pushMessageToSingle($message, $target,$requstId);
	        var_dump($rep);
	        echo ("<br><br>");
	    }
	}

	public function IGtNotyPopLoadTemplateDemo(){
        $template =  new \IGtNotyPopLoadTemplate();
        $template ->set_appId($this->APPID);                      //应用appid
        $template ->set_appkey($this->APPKEY);                    //应用appkey
        //通知栏
        $template ->set_notyTitle("个推单推测试111");                 //通知栏标题
        $template ->set_notyContent("最新版点击下载111"); //通知栏内容
        $template ->set_notyIcon("");                      //通知栏logo
        $template ->set_isBelled(true);                    //是否响铃
        $template ->set_isVibrationed(true);               //是否震动
        $template ->set_isCleared(true);                   //通知栏是否可清除
        //弹框
        $template ->set_popTitle("弹框标题");              //弹框标题
        $template ->set_popContent("弹框内容");            //弹框内容
        $template ->set_popImage("");                      //弹框图片
        $template ->set_popButton1("下载");                //左键
        $template ->set_popButton2("取消");                //右键
        //下载
        $template ->set_loadIcon("");                      //弹框图片
        $template ->set_loadTitle("地震速报下载");
        $template ->set_loadUrl("http://dizhensubao.igexin.com/dl/com.ceic.apk");
        $template ->set_isAutoInstall(false);
        $template ->set_isActived(true);

        //设置通知定时展示时间，结束时间与开始时间相差需大于6分钟，消息推送后，客户端将在指定时间差内展示消息（误差6分钟）
        $begin = "2016-09-30 09:39:22";
        $end = "2016-09-30 09:45:24";
        $template->set_duration($begin,$end);
        return $template;
	}

	public function IGtNotificationTemplateDemo($APPID,$APPKEY,$title='',$text=''){
	    $template =  new \IGtNotificationTemplate();
	    $template->set_appId($APPID);                   //应用appid
	    $template->set_appkey($APPKEY);                 //应用appkey
	    $template->set_transmissionType(1);            //透传消息类型
	    $template->set_transmissionContent('测试离线啊啊啊');//透传内容
	    $template->set_title($title);                  //通知栏标题
	    $template->set_text($text);     //通知栏内容
	    $template->set_logo('');                       //通知栏logo
	    $template->set_logoURL('');                    //通知栏logo链接
	    $template->set_isRing(false);                   //是否响铃
	    $template->set_isVibrate(false);                //是否震动
	    $template->set_isClearable(true);              //通知栏是否可清除

	    return $template;
	}
}